package com.work.demo.controller;

import com.work.demo.entity.Product;
import com.work.demo.entity.ResultResponce;
import com.work.demo.service.impl.ProductServiceImpl;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired(required = false)
    @Qualifier(value = "productService")
    private ProductServiceImpl service;

    /**
     * 添加产品信息
     * @param product
     * @param imgFile
     * @param useFile
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/addProduct")
    @ResponseBody
    private ResultResponce addProduct(Product product, MultipartFile imgFile, MultipartFile useFile, HttpServletRequest request) throws IOException {
        if (product == null || imgFile == null || useFile == null) {
            return new ResultResponce<String>(null, -1, "属性或文件不能为空", false);
        }
        //生成id
        String pid = UUID.randomUUID().toString();
        product.setPid(pid);
        //存储图片并设置产品img的路径
        String imgName = saveFile(imgFile, "images", pid, request);
        if (imgName == null) return new ResultResponce<String>(null, -1, "图片保存失败！", false);
        product.setImg("images/" +imgName);
        //存储使用说明书并设置其路径
        String useName = saveFile(useFile, "instructions", pid, request);
        if (useName == null) return new ResultResponce<String>(null, -1, "文件保存失败！", false);
        product.setInstruction("instructions/" +useName);

        //存储数据库
        System.out.println(product);
        int insertFlag = service.insert(product);
        //判断是否插入成功
        if(insertFlag>0){
            //给页面绑定数据
            request.setAttribute("product",product);
        }else {
            //给页面绑定数据
            request.setAttribute("product",new Product());
        }
        return new ResultResponce(product, 1, "成功！", true);
    }


    /**
     * 存储文件的自定义方法
     * @param file
     * @param pid
     * @param request
     * @return
     * @throws IOException
     */
    private String saveFile(MultipartFile file, String address, String pid,HttpServletRequest request) throws IOException {
        //截取文件后缀名,生成新的文件名字
        if (file == null) return null;
        String filename = file.getOriginalFilename();
        if (filename == null || "".equals(filename)) return null;
        String extendName = filename.substring(filename.lastIndexOf("."));
        String fileSaveName= pid+"_"+System.currentTimeMillis()+extendName;
        //获取目录在服务器的路径
        String fileSavePath = request.getServletContext().getRealPath(address);
        //构建存储路径
        String savePath =fileSavePath+"/"+fileSaveName;
        //存储文件
        boolean res = updateFile(file, savePath);
        if (res)  return fileSaveName;
        else return null;
    }

    //上传文件
    public boolean updateFile(MultipartFile file, String path) {
        boolean result = false;
        try {
            //将图片保存
            System.out.println(path);
            file.transferTo(new File(path));
            result = true;
        } catch (IOException e) {
            System.out.println("图片上传失败!" + e.getMessage());
        }
        return result;
    }
}
